﻿using System;
using System.Reflection.Metadata;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Ocelot.DependencyInjection;
using Spark.Config;

namespace Spark.Samples.ApiGateway
{
    public class Constant
    {
        public static readonly string[] ConfigList = new string[] { "GlobalConfig", "SamplesGatewayConfig" };
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(
                    (hostContext, builder) =>
                    {
                        foreach (var item in Constant.ConfigList)
                        {
                            builder.AddRemoteConfig(
                                options =>
                                {
                                    options.AppCode = Environment.GetEnvironmentVariable("AppCode");
                                    options.Key = item;
                                    options.Optional = true;
                                    options.ReloadOnChange = true;
                                    options.Url = Environment.GetEnvironmentVariable("ConfigUrl");
                                    options.Interval = 1000 * 60;
                                });
                        }
                    })
                .UseStartup<Startup>();
    }
}